﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>劳动合同</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="~/lib/layui/css/layui.css" media="all">
    <script src="~/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
        <form lay-filter="ldht" class="layui-form layui-form-pane" action="">
            <input type="hidden" name="id">
            <input type="hidden" name="jbxxid">
            <!--合同类型-->
            <div class="layui-form-item">
                <div class="layui-inline">
                <label class="layui-form-label" style="width:150px">合同类型:</label>
                    <div class="layui-input-inline">
                        <select name="type" lay-verify="required" lay-search="" lay-filter="htype">
                            <option value="">下拉选择合同签订类型</option>
                            <option value="首次签订劳动合同">首次签订劳动合同</option>
                            <option value="二次签订劳动合同">二次签订劳动合同</option>
                            <option value="无固定期限劳动合同">无固定期限劳动合同</option>
                        </select>
                    </div>
                </div>
            <hr class="layui-border-green">
            <!--合同期限-->
            <div class="layui-inline">
                <label class="layui-form-label" style="width:150px">合同期限（年）：</label>
                <div class="layui-input-inline">
                    @*<input type="text" name="year" class="layui-input">*@
                    <select name="year" lay-verify="required" lay-search="">
                        <option value="">下拉选择年限</option>
                        <option value="1年">1年</option>
                        <option value="2年">2年</option>
                        <option value="3年">3年</option>
                        <option value="4年">4年</option>
                        <option value="5年">5年</option>
                        <option value="6年">6年</option>
                        <option value="7年">7年</option>
                        <option value="8年">8年</option>
                        <option value="9年">9年</option>
                        <option value="10年">10年</option>
                        <option value="无固定期限">无固定期限</option>
                    </select>
                </div>
            </div>
            <hr class="layui-border-green">
            <!--起止时间-->
            <div class="layui-inline">
                <label class="layui-form-label" style="width:150px">开始时间:</label>
                <div class="layui-input-inline">
                    <input type="text" name="sdate" id="date" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline" id="e_date" style="display:none">
                <label class="layui-form-label" style="width:150px">结束时间:</label>
                <div class="layui-input-inline">
                    <input type="text" name="edate" id="date" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
            </div>
            <hr class="layui-border-green">
            <div class="layui-form-item">
                  <button type="button" class="layui-btn" id="Save_ldht">保存</button>
            </div>
            <hr class="layui-border-green">
        </form>
        <script>
        layui.use(['form', 'util', 'laydate','table'], function () {
            var form = layui.form;
            var layer = layui.layer;
            var util = layui.util;
            var laydate = layui.laydate;
            var table = layui.table;

            var url = location.search; 
            if (url.indexOf("?") != -1) {  
                var id = getQueryVariable('id');
                var jbxxid = getQueryVariable('jbxxid');

                if(id!=null)
                {
                    var data = gethtData("", id, "last");
                    form.val('ldht', {
                        "id": id,
                        "jbxxid": data[0].jbxxid,
                        "type": data[0].type,
                        "sdate": data[0].sdate,
                        "edate": data[0].edate,
                        "year": data[0].year
                    });

                    if (data[0].type == "无固定期限劳动合同") {
                        $("#e_date").hide();
                    } else {
                        $("#e_date").show();
                    }

                }else
                {
                    form.val('ldht', {
                        "jbxxid": jbxxid
                    });
                }
            }

            //常规用法
            laydate.render({
                elem: '#date'
            });

            form.on('select(htype)', function (data) {
                if (data.value == "无固定期限劳动合同") {
                    $("#e_date").hide();
                    form.val('ldht', {
                        "year": "无固定期限"
                    });
                }
                else {
                    $("#e_date").show();
                    form.val('ldht', {
                        "year": ""
                    });
                }
            });

            //劳动合同保存
            layui.$('#Save_ldht').on('click', function () {
                var data = form.val('ldht');

                if (data.jbxxid == "") {
                    layer.msg("请先保存基本信息!");
                    return;
                }

                if (data.type == "") {
                    layer.msg("请选择合同类型！");
                    return;
                }

                if (data.year == "") {
                    layer.msg("请选择合同年限！");
                    return;
                }

                if (data.sdate == "") {
                    layer.msg("请选择开始时间！");
                    return;
                }

                if (data.type != "无固定期限劳动合同" & data.edate == "") {
                    layer.msg("请选择结束时间！");
                    return;
                }

                var opurl = "/HmcWH/Save_ldht";
                if (form.val('ldht').id != "") {
                    opurl = "/HmcWH/Update_ldht";
                } else {
                    form.val('ldht', {
                        "id": guiid()
                    });
                }
                data = form.val('ldht');

                $.ajax({
                    url: opurl,
                    type: "post",
                    async: false,
                    data: data,
                    success: function (result) {
                        var index = parent.layer.getFrameIndex(window.name);
                        layer.msg(result, { time: 1000 });
                        if (result == "保存成功！" || result == "修改成功！") {
                            //刷新父级表格
                            parent.layui.table.reload('oldht', {
                                data: gethtData(data.jbxxid, "", "")
                            });
                        }
                        if(result=="保存失败！"){
                            form.val('ldht', {
                                "id": ""
                            });
                        }
                        parent.layer.close(index);
                    }
                });                                            
            });

        });

        function gethtData(jbxxid,id,type) {
            var data = [];
            $.ajax({
                url: "/HmcWH/get_ldht",    //后台数据请求地址
                type: "post",
                async: false,
                data:{"jbxxid":jbxxid,"ldhtid":id,"type":type},
                success: function (resut) {
                    data = resut;
                }
            });
            return data;
        }

        function guiid() {
            return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
                function (c) {
                    var r = (Math.random() * 16) | 0,
                        v = c == 'x' ? r : (r & 0x3) | 0x8;
                    return v.toString(16);
                }
            );
        }

        function getNow() {
            var nowdate = new Date();
            var year = nowdate.getFullYear(),
                month = nowdate.getMonth() + 1,
                date = nowdate.getDate();

            return year+"-"+month+"-"+date;
        }

        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) { return pair[1]; }
            }
            return null;
        }
        </script>

</body>
</html>